//
//  SLTestController.m
//  Objective-C
//
//  Created by S_Line on 2018/8/21.
//  Copyright © 2018年 S_Line. All rights reserved.
//

#import "SLTestController.h"

#import <CoreTelephony/CTCallCenter.h>
#import <CoreTelephony/CTCall.h>

#import "SLTestCell.h"
#import "SLTextTableHeaderView.h"

#import "NSObject+Calculate.h"

#import <CoreLocation/CoreLocation.h>

@interface SLTestController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) CTCallCenter *callCenter;

@end

@implementation SLTestController

- (void)initNavBar {
    self.navTitle = @"测试页面";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavBar];
    
    [self initData];
    
    [self testTableView];
//    [self testFrameAndMasonry];
}

- (void)initData {
//    [self addNotification];
    
    [self initCallCenter];
    NSDictionary *dict = [NSDictionary dictionary];
    NSString *str = [dict objectForKey:@"str"];
    NSString *str1 = nil;
    SLog(@"str: %@, str1: %@", str, str1);
    
    CGFloat result = [NSObject makeCalculators:^(CalculatorMaker * _Nonnull maker) {
        maker.add(1).add(2).add(3).multi(2);
    }];
    NSLog(@"--------> %f <--------", result);
    
    
}

- (void)testFrameAndMasonry {
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    backView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:backView];
    __weak typeof(backView) weakBackView = backView;
    backView.tapBlock = ^{
        [UIView animateWithDuration:0.35 animations:^{
            weakBackView.frame = CGRectMake(100, 100, 200, 200);
        }];
    };
    
    UIView *masonryView = [[UIView alloc] init];
    masonryView.backgroundColor = [UIColor redColor];
    [backView addSubview:masonryView];
    [masonryView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(backView.mas_centerY).mas_offset(0);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(40);
    }];
}

- (void)testTableView {
    UITableView *tableView = [[UITableView alloc] init];
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView registerClass:[SLTestCell class] forCellReuseIdentifier:@"SLTestCell"];
    [self.view addSubview:tableView];
    [tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.mas_equalTo(0);
    }];
}

- (void)initCallCenter {
    CTCallCenter *callCenter = [[CTCallCenter alloc] init];
    self.callCenter = callCenter;
    callCenter.callEventHandler = ^(CTCall* call) {
        if ([call.callState isEqualToString:CTCallStateDisconnected])
        {
            NSLog(@"挂断电话Call has been disconnected");
        }
        else if ([call.callState isEqualToString:CTCallStateConnected])
        {
            NSLog(@"电话通了Call has just been connected");
        }
        else if([call.callState isEqualToString:CTCallStateIncoming])
        {
            NSLog(@"来电话了Call is incoming");
            
        }
        else if ([call.callState isEqualToString:CTCallStateDialing])
        {
            NSLog(@"正在拨出电话call is dialing");
        }
        else
        {
            NSLog(@"什么没做Nothing is done");
        }
    };
}

- (void)initView {
    __weak typeof(self) weakself = self;
    self.view.tapBlock = ^{
        [weakself.view endEditing:true];
    };
    
    UITextField *textField = [[UITextField alloc] init];
    textField.borderStyle = UITextBorderStyleLine;
    textField.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:textField];
    [textField mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kMargin_Horizontal);
        make.right.mas_equalTo(-kMargin_Horizontal);
        make.top.mas_equalTo(kMargin_Ten_Point + kNavigationBarHeight);
    }];
}

- (void)testTap {
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 300, 100)];
    backView.backgroundColor = [UIColor redColor];
    [self.view addSubview:backView];
    UITapGestureRecognizer *backViewTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backViewTap)];
    [backView addGestureRecognizer:backViewTap];
    
    UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 80)];
    buttonView.backgroundColor = [UIColor blueColor];
    [backView addSubview:buttonView];
    UITapGestureRecognizer *buttonViewTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonViewTap)];
    [buttonView addGestureRecognizer:buttonViewTap];
    
    UIButton *actionButton = [[UIButton alloc] initWithFrame:CGRectMake(120, 10, 100, 80)];
    [actionButton setTitle:@"actionButton" forState:UIControlStateNormal];
    [actionButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    actionButton.backgroundColor = [UIColor lightGrayColor];
    [actionButton addTarget:self action:@selector(actionButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:actionButton];
}

- (void)backViewTap {
    NSLog(@"----------> backViewTap <----------");
}

- (void)buttonViewTap {
    NSLog(@"----------> buttonViewTap <----------");
    NSMutableString * string = [[NSMutableString alloc] initWithFormat:@"telprompt:%@",@"18964151472"];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];
}

- (void)actionButtonClick {
    NSLog(@"----------> actionButtonClick <----------");
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

//- (void)findInputViewInView:(UIView *)view {
//    if ([view isKindOfClass:[UITextField class]] || [view isKindOfClass:[UITextView class]]) {
//        [self setInputAccessoryViewForInputView:view];
//    } else if (view.subviews && view.subviews.count > 0) {
//        for (UIView *subview in view.subviews) {
//            [self findInputViewInView:subview];
//        }
//    }
//}
//
//- (void)setInputAccessoryViewForInputView:(UIView *)inputView {
//    CGFloat inputAccessoryViewH = 40;
//    UIView *inputAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, inputAccessoryViewH)];
//    inputAccessoryView.backgroundColor = [UIColor colorWithHexString:@"#f8f8f8"];
//    UIView *separatorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.5)];
//    separatorView.backgroundColor = [UIColor colorWithHexString:@"#ccd0d6"];
//    [inputAccessoryView addSubview:separatorView];
//    CGFloat finishButtonW = 40;
//    CGFloat finishButtonX = kScreenWidth - finishButtonW - 25;
//    UIButton *finishButton = [[UIButton alloc] initWithFrame:CGRectMake(finishButtonX, 0, finishButtonW, inputAccessoryViewH)];
//    finishButton.titleLabel.font = [UIFont systemFontOfSize:15];
//    [finishButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [finishButton setTitle:@"完成" forState:UIControlStateNormal];
//    [finishButton addTarget:self action:@selector(endEdit) forControlEvents:UIControlEventTouchUpInside];
//    [inputAccessoryView addSubview:finishButton];
//
//    UIKeyboardType keyboardType = UIKeyboardTypeDefault;
//    if ([inputView isKindOfClass:[UITextField class]]) {
//        UITextField *textField = (UITextField *)inputView;
//        keyboardType = textField.keyboardType;
//    } else  if ([inputView isKindOfClass:[UITextView class]]) {
//        UITextView *textView = (UITextView *)inputView;
//        keyboardType = textView.keyboardType;
//    }
//
//    if (keyboardType == UIKeyboardTypeNumberPad || keyboardType == UIKeyboardTypeDecimalPad || keyboardType == 5 || keyboardType == UIKeyboardTypePhonePad) {
//        if ([inputView isKindOfClass:[UITextField class]]) {
//            dispatch_async(dispatch_get_main_queue(), ^{
//                ((UITextField *)inputView).inputAccessoryView = inputAccessoryView;
//            });
//        } else if ([inputView isKindOfClass:[UITextView class]]) {
//            ((UITextView *)inputView).inputAccessoryView = inputAccessoryView;
//        }
//    }
//}
//
//- (void)addNotification {
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
//}
//
//- (void)removeObserver {
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
//}
//
//- (void)keyboardWillShow:(NSNotification *)noti {
//    SLog(@"keyboardWillShow - %@", [self class]);
//    [self showDismissView:noti];
//}
//
//- (void)keyboardDidShow:(NSNotification *)noti {
//    [self showDismissView:noti];
//}
//
//- (void)keyboardWillHide:(NSNotification *)noti {
//    SLog(@"keyboardWillHide - %@", [self class]);
//}
//
//- (void)showDismissView:(NSNotification *)noti {
//    NSLog(@"showDismissView");
//    //    NSValue *keyboardFrameValue = [noti.userInfo objectForKey:@"UIKeyboardFrameEndUserInfoKey"];
//    //    CGRect keyboardFrame = [keyboardFrameValue CGRectValue];
//
//    UIKeyboardType type = UIKeyboardTypeDefault;
//    UIView *inputView = [self findFirstResponder:self.view];
//
//    CGFloat inputAccessoryViewH = 40;
//    UIView *inputAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, inputAccessoryViewH)];
//    inputAccessoryView.backgroundColor = [UIColor colorWithHexString:@"#f8f8f8"];
//    UIView *separatorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.5)];
//    separatorView.backgroundColor = [UIColor colorWithHexString:@"#ccd0d6"];
//    [inputAccessoryView addSubview:separatorView];
//    CGFloat finishButtonW = 40;
//    CGFloat finishButtonX = kScreenWidth - finishButtonW - 25;
//    UIButton *finishButton = [[UIButton alloc] initWithFrame:CGRectMake(finishButtonX, 0, finishButtonW, inputAccessoryViewH)];
//    finishButton.titleLabel.font = [UIFont systemFontOfSize:15];
//    [finishButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [finishButton setTitle:@"完成" forState:UIControlStateNormal];
//    [finishButton addTarget:self action:@selector(endEdit) forControlEvents:UIControlEventTouchUpInside];
//    [inputAccessoryView addSubview:finishButton];
//
//    if (inputView) {
//        if ([inputView isKindOfClass:[UITextField class]]) {
//            type = ((UITextField *)inputView).keyboardType;
//        } else if ([inputView isKindOfClass:[UITextView class]]) {
//            type = ((UITextView *)inputView).keyboardType;
//        }
//    }
//    if (type == UIKeyboardTypeNumberPad || type == UIKeyboardTypeDecimalPad || type == 5 || type == UIKeyboardTypePhonePad) {
//        if ([inputView isKindOfClass:[UITextField class]]) {
//            NSLog(@"inputAccessoryView.frame: %@ - type: %ld", NSStringFromCGRect(inputAccessoryView.frame), type);
//            dispatch_async(dispatch_get_main_queue(), ^{
//                ((UITextField *)inputView).inputAccessoryView = inputAccessoryView;
//            });
//        } else if ([inputView isKindOfClass:[UITextView class]]) {
//            ((UITextView *)inputView).inputAccessoryView = inputAccessoryView;
//        }
//    }
//}
//
//- (UIView *)findFirstResponder:(UIView *)view {
//
//    UIView *firstResponder;
//    for (int i = 0; i < view.subviews.count; i++) {
//        UIView *subView = view.subviews[i];
//
//        //        NSString *subViewClassStr = NSStringFromClass([subView class]);
//        //        NSLog(@"%@", subViewClassStr);
//        if ([self testIsFirstResponder:subView]) {
//            return subView;
//        } else {
//            if (subView.subviews && view.subviews.count > 0) {
//                UIView *tempView = [self findFirstResponder:subView];
//                if (tempView && [self testIsFirstResponder:tempView]) {
//                    return tempView;
//                }
//            }
//        }
//    }
//
//    return firstResponder;
//}
//
//- (BOOL)testIsFirstResponder:(UIView *)view {
//    BOOL isFirstResponder = false;
//
//    if ([view isKindOfClass:[UITextField class]]) {
//        if (view.isFirstResponder) {
//            isFirstResponder = true;
//        }
//    } else if ([view isKindOfClass:[UITextView class]]) {
//        if (view.isFirstResponder) {
//            isFirstResponder = true;
//        }
//    }
//
//    return isFirstResponder;
//}

#pragma mark ----- UITableView DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SLTestCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SLTestCell"];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 95;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SLTextTableHeaderView *headerView = [[SLTextTableHeaderView alloc] init];
    tableView.tableHeaderView = headerView;
    [headerView setData];
    [headerView layoutIfNeeded];
}

- (void)endEdit {
    [self.view endEditing:true];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
